<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/22 0022
 * Time: 10:01
 */

namespace Cohuna\Services;


use Cohuna\Models\CohunaMerchant;
use Cohuna\Models\CohunaMerchantPay;
use Cohuna\Models\CohunaPay;
use Cohuna\Payment\PaymentFactory;
use Phalcon\Di;

/**
 * 支付相关业务
 * @package Cohuna\Services
 */
class PayService extends ServiceBase
{
    /**
     * @var static
     */
    protected static $instance = null;


    /**
     * @param array $params
     * @throws \Exception
     */
    public function createAlipayTrade(array $params)
    {
        $this->checkCommonParams($params);

        $pay = CohunaPay::findFirst(array(
            "merchant_id = :merchant_id: AND pay_id=:pay_id: AND is_delete = 0 AND pay_status = 'y'",
            'bind' => array('merchant_id'=>$params['mch_id'],'pay_id' => $params['pay_id'])
        ));

        if(empty($pay)){
            throw new \Exception('指定的支付方式不存在或已禁用');
        }
        $config = Di::getDefault()->getShared('config')->payment_platform_list;

        $channel_list = $config[$pay->pay_platform]['gateway_list'][$pay->gateway]['channel']->toArray();

        //检查是否支持指定的渠道
        if(!in_array($params['trade_channel'],$channel_list)){
            throw new \Exception('指定的支付方式不支持指定的渠道');
        }

        //创建订单
        $cohuna_order = CohunaOrderService::getInstance()->createOrder($params);

        if($cohuna_order === false){
            throw new \Exception('创建订单失败');
        }


        $pay_config = $pay->pay_config;
        $pay_config['pay_id']           = $pay->pay_id;
        $pay_config['sign_type']        = $pay->sign_type;
        $pay_config['trade_channel']    = $params['trade_channel'];
        $pay_config['gateway']          = $pay->gateway;

        $biz_content = (array)$cohuna_order;



        $response = PaymentFactory::createPayment($pay_config,$biz_content);

        return $response;

    }

    /**
     * 检查公共参数
     * @param array $params
     * @return array|bool
     */
    public function checkCommonParams(array $params)
    {
        if(empty($params)){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'参数非法');
        }
        if(!isset($params['mch_id']) || empty($params['mch_id'])){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'缺少参数【mch_id】');
        }
        if(!isset($params['pay_id']) || empty($params['pay_id'])){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'缺少参数【pay_id】');
        }
        if(!isset($params['nonce_str']) || empty($params['nonce_str'])){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'缺少参数【nonce_str】');
        }
        if(!isset($params['sign']) || empty($params['sign'])){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'缺少参数【sign】');
        }
        if(!isset($params['order_no']) || empty($params['order_no'])){
            return array('errCode' => 'INVALID_PARAMETER','errMsg'=>'缺少参数【order_no】');
        }
        $conditions = array(
            'merchant_id=:merchant_id: AND is_delete=0 AND merchant_status = :status:',
            'bind'=>array(
                'merchant_id'=>$params['mch_id'],
                'status'=>'y'
            ));

        if(CohunaMerchant::count($conditions) <= 0){
            return array('errCode' => 'MERCHANT_NOT_EXIST','errMsg'=>'商户不存在');
        }
        $conditions = array(
            "pay_id = :pay_id: AND is_delete = 0 AND pay_status='y'",
            'bind' => array(
                'pay_id' => $params['pay_id']
            )
        );
        if(CohunaPay::count($conditions) <= 0){
            return array('errCode' => 'PAY_NOT_EXIST','errMsg'=>'支付配置不存在');
        }
        $conditions = array(
            'merchant_id=:merchant_id: AND pay_id = :pay_id:',
            'bind' => array(
                'merchant_id'=>$params['mch_id'],
                'pay_id' => $params['pay_id']
            )
        );

        if(CohunaMerchantPay::count($conditions) <= 0){
            return array('errCode' => 'MERCHANT_PAY_NOT_EXIST','errMsg'=>'商户的支付配置不存在');
        }

        return true;
    }




}